using Kassandra.Core.Repositories;
using Kassandra.WindowsUI;
using Kassandra.WindowsUI.Presenters;
using NUnit.Framework;
using Rhino.Mocks;

namespace Kassandra.Tests.WindowsUI
{
    public class AddNewImagePresenterTests
    {
        [TestFixture]
        public class When_saving_an_image
        {
            private AddNewImagePresenter _addNewImagePresenter;
            private MockRepository _mockery;
            private ICheckForExistingImageByAssignedCharacter _mockExistanceCheck;
            private IImageRepository _mockImageRepository;
            private IAddNewImageView _addNewImageView;

            [SetUp]
            public void Setup_Context()
            {
                _mockery = new MockRepository();
                _mockExistanceCheck = _mockery.DynamicMock<ICheckForExistingImageByAssignedCharacter>();
                _mockImageRepository = _mockery.DynamicMock<IImageRepository>();
                _addNewImageView = _mockery.DynamicMock<IAddNewImageView>(); 
                _addNewImagePresenter = new AddNewImagePresenter(_mockExistanceCheck, _mockImageRepository, _addNewImageView);
            }

            [Test]
            public void should_not_save_image_when_character_is_already_associated()
            {
                SetupResult.For(_mockExistanceCheck.AgainstTheCharacter('c')).IgnoreArguments().Return(true);
                using (_mockery.Record())
                {
                    _mockImageRepository.Save(string.Empty, 'c');
                    LastCall.IgnoreArguments().Repeat.Never();
                }

                using (_mockery.Playback())
                {
                    _addNewImagePresenter.Save(string.Empty, 'c');
                }
            }

            [Test]
            public void should_persist_the_image_to_the_data_store()
            {
                SetupResult.For(_mockExistanceCheck.AgainstTheCharacter('c')).IgnoreArguments().Return(false);
                using (_mockery.Record())
                {
                    _mockImageRepository.Save(string.Empty, 'c');
                    LastCall.IgnoreArguments().Repeat.Once();
                }

                using (_mockery.Playback())
                {
                    _addNewImagePresenter.Save("asdf", 'c');
                }
            }

            [Test]
            public void should_check_for_an_existing_file()
            {
                using (_mockery.Record())
                {
                    Expect.Call(_mockExistanceCheck.AgainstTheCharacter('c')).IgnoreArguments().Return(false).Repeat.Once
                        ();
                }

                using (_mockery.Playback())
                {
                    _addNewImagePresenter.Save("asfd", 'c');
                }
            }
        }
    }
}